package com.distribute.application.api.rest.delivery;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.distribute.application.api.BaseApiController;
import com.distribute.framework.core.lang.Protocol;
import com.distribute.framework.core.lang.title;
import com.distribute.framework.core.web.helper.Utils;
import com.distribute.framework.model.Account;
import com.distribute.framework.model.Distributor;
import com.distribute.framework.model.DistributorCash;
import com.distribute.framework.model.DistributorIncome;
import com.distribute.framework.model.Member;
import com.distribute.framework.model.MembershipRecord;
import com.distribute.framework.model.TbMsg;
@RestController(value="api.DeliveryIncomeController")
public class DeliveryIncomeController  extends BaseApiController{
	/**
	 * 配送商获取收益列表
	 * @return
	 */
	@RequestMapping("/api/delivery/getDeliveryIncomes.do")
	public @title("配送商获取收益列表") Protocol getDeliveryIncomes(
			@title("formId") String formId,
			@title("用户编号") Long userId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		
		Distributor distributor=Distributor.where.member(member.getId()).get();
		
		List<DistributorIncome> list=DistributorIncome.where.distributor(distributor.getId()).list(10000);
		Double a=0.0;
		Double b=0.0;
		Double c=0.0;
		for (DistributorIncome incomeRecord : list) {
			a=a+incomeRecord.getDistriIncome();
			if (incomeRecord.getOrderStatus()==(short)1) {
				b=b+incomeRecord.getDistriIncome();
			}else if (incomeRecord.getOrderStatus()==(short)2) {
				c=c+incomeRecord.getDistriIncome();
			}
		}
		protocol.set("asum", a);
		protocol.set("bsum", b);
		protocol.set("csum", c);
		return protocol;
	}
	/**
	 * 配送商查看自己的收益
	 * @return
	 */
	@RequestMapping("/api/delivery/getDeliverryProfitLists.do")
	public @title("配送商查看自己的收益") Protocol getDeliverryProfitLists(
			@title("类型") Short type
			,@title("页码") Integer pageNo
			,@title("个数") Integer pageSize
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Distributor distributor=Distributor.where.member(member.getId()).get();
		DistributorIncome.M where = DistributorIncome.where.distributor(distributor.getId());
		if(type!=(short)0){
			where.orderStatus(type);
		}
		protocol.setPage(where.page(Utils.or(pageNo,0), 1000), new Protocol.BeanFormat<DistributorIncome>(){
			@Override
			public Map format(DistributorIncome t) {
				Map json = t.toJSON();
				json.put("mber", Member.where.uid(t.getOrderPerson()).get().toJSON());
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 配送商查看自己提现记录
	 * @return
	 */
	@RequestMapping("/api/delivery/getDelMoneyLists.do")
	public @title("配送商查看自己提现记录") Protocol getDelMoneyLists(
			@title("页码") Integer pageNo
			,@title("个数") Integer pageSize
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Distributor distributor=Distributor.where.member(member.getId()).get();
		protocol.setPage(DistributorCash.where.distributor(distributor.getId()).page(Utils.or(pageNo,0), Utils.or(pageSize,20)), new Protocol.BeanFormat<DistributorCash>(){
			@Override
			public Map format(DistributorCash t) {
				Map json = t.toJSON();
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 配送商添加提现记录
	 * @return
	 */
	@RequestMapping("/api/delivery/getDelMoneyRecords.do")
	public @title("配送商添加提现记录") Protocol getDelMoneyRecords(
			@title("金额") Double money
			,@title("规则") Short gz
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Account account=Account.where.member(member.getId()).get();
//		if(!super.getSession().getAttribute(account.getContactNumber()).equals(code)){
//			protocol.set("msg", "验证码错误");
//			return protocol;
//		}
		if (money>account.getMoney()) {
			protocol.set("msg", "提现金额不足");
			return protocol;
		}
		Distributor distributor=Distributor.where.member(member.getId()).get();
		if (money>5000) {
			protocol.set("msg", "每人每天提现上限5000元");
			protocol.set("type", 2);
			return protocol;
		}
		List<DistributorCash> membershipRecords=DistributorCash.where.distributor(distributor.getId()).approvalStatus((short)1).list(-1);
		if (membershipRecords.size()>0) {
			protocol.set("msg", "您还有未审核通过的提现申请");
			protocol.set("type", 2);
			return protocol;
		}
		DistributorCash sRecord=new DistributorCash(
				distributor.getId()//配送商 [非空]
			 	,member.getNickName()//联系人 [非空]
			 	,member.getMobile()//手机号 [非空]
			 	,money//申请金额 [非空]
			 	,""//支付宝名称 [非空]
			 	,""//支付宝账号 [非空]
			 	,""
			 	,(short)1//打款状态 [非空]
			 	,""
			 	,""//拒绝理由
				,new Date()//修改时间 [非空]
			 	,new Date()//添加时间 [非空]
			 	,(short)1//状态 [非空]
				);
		sRecord.save();
		account.setMoney(account.getMoney()-money);
		account.update();
		TbMsg msg=new TbMsg(
				 distributor.getMember()//会员
					 	,"您发起了提现申请:"+money//内容
					 	,new Date()//时间
						);
				msg.save();
		protocol.set("msg", "提交成功！等待审核");
		return protocol;
	}
	/**
	 * 配送商获取提现账户信息
	 * @return
	 */
	@RequestMapping("/api/delivery/delGetsAccount.do")
	public @title("配送商获取提现账户信息") Protocol delGetsAccount(
			@title("用户编号") Long userId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Account account=Account.where.member(member.getId()).get();
		protocol.set("account", account.toJSON());
		return protocol;
	}
}
